import type { IQuery, Response, TableResponse } from '@/api/config/types';
import { exportFile, fileUploader } from '@/api/utils/file';
import { message } from 'ant-design-vue';
import { SystemStatus } from '@/global/enum/status.enum';
import axios from 'axios';
import type { SystemConfig } from './types';
import type { PendingUpdates } from '@/types/system';
import type { SYSTEM_CONFIG } from '@/global/enum/common';
import type { SysConfig } from '@/views/modules/tool/system-setting/data/types';

/**
 * @description: 统一管理API前缀，方便修改
 */
const REQUEST_PREFIX = 'system/config';

/**
 * @description: 查询参数配置列表
 * @param {IQuery} query 系统通用查询对象
 */
export const fetchConfigList = (query: IQuery) => {
  return axios.get<TableResponse<SystemConfig>>(`${REQUEST_PREFIX}/list`, {
    params: query,
  });
};

/**
 * @description: 查询参数配置详细
 * @param {number} configId 主键ID
 */
export const fetchConfigById = (configId: number | string) => {
  return axios.get<Response<SystemConfig>>(`${REQUEST_PREFIX}/${configId}`);
};

/**
 * @description: 新增参数配置
 * @param {SystemConfig} data 需要添加的数据
 */
export const createConfig = (data: SystemConfig) => {
  return axios.post<Response<SystemConfig>>(`${REQUEST_PREFIX}`, data);
};

/**
 * @description: 更新参数配置
 * @param {SystemConfig} data 更新数据
 */
export const updateConfig = (data: SystemConfig) => {
  return axios.put<Response<SystemConfig>>(`${REQUEST_PREFIX}`, data);
};

/**
 * @description: 删除参数配置
 * @param {number[]} configIds 传入ID数组,可以删除多个
 */
export const deleteConfig = (configIds: (number | string)[]) => {
  return axios.delete<Response<SystemConfig>>(`${REQUEST_PREFIX}/${configIds.join(',')}`);
};

/**
 * @description: 批量新增参数配置
 * @param {SystemConfig[]} list
 */
export const batchCreateConfig = (list: SystemConfig[]) => {
  return axios.post<Response>(`${REQUEST_PREFIX}/batch`, list);
};
/**
 * @description: 加载所有参数配置
 */
export const loadAllConfig = () => {
  return axios.get<TableResponse<SystemConfig>>(`${REQUEST_PREFIX}/loadAll`);
};

/**
 * @description: 导出参数配置
 */
export const exportConfig = async (query: IQuery) => {
  try {
    await exportFile({
      url: `${REQUEST_PREFIX}/export`,
      data: query,
      fileName: '参数配置.xls',
      method: 'POST',
    });
    message.success(`成功下载`);
  } catch (e) {
    message.success(`下载失败`);
  }
};
/**
 * @description: 三向绑定 前端-后端-数据库 参数配置
 */
export const updateConfigAttr = (config: PendingUpdates) => {
  return axios.post<Response<SystemConfig>>(`${REQUEST_PREFIX}/updateAttr`, { ...config });
};

/**
 * @description: 下载模板,用于批量导入测试表
 */
export const importConfig = async (file: File) => {
  try {
    const data = {
      file,
      path: file.name,
    };
    const { response } = await fileUploader({
      files: [data],
      url: `${REQUEST_PREFIX}/importData`,
    });
    if (response.code === SystemStatus.SUCCESS) {
      message.success('上传成功');
    }
  } catch (e) {
    message.success(`上传失败`);
  }
};
/**
 * @description: 刷新缓存
 */
export const refreshCache = () => {
  return axios.delete<Response<SystemConfig>>(`system/config/refreshCache`);
};

type Page = {
  pageSize: number;
  pageNum: number;
};

/**
 * @description: 加密存储
 */
export const encryptedStorage = (key: SYSTEM_CONFIG, value: string) => {
  return axios.post<Response<SystemConfig>>(`system/config/encryptedStorage`, {
    configKey: key,
    configValue: value,
  });
};

/**
 * @description : 查询参数配置详细
 * @param {string} key 主键ID
 */
export const fetchConfigByKey = (key: SYSTEM_CONFIG) => {
  return axios.get<Response<SysConfig>>(`system/config/configKey/${key}`);
};
